package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.CategoryDao;
import com.chengxiaoxiao.domain.Category;
import com.chengxiaoxiao.utils.db.BeanHandler;
import com.chengxiaoxiao.utils.db.BeanListHandler;
import com.chengxiaoxiao.utils.db.DBAssist;
import com.chengxiaoxiao.utils.db.DBCPUtil;

import java.util.List;

public class CategoryDaoImpl implements CategoryDao {

    private DBAssist da = new DBAssist(DBCPUtil.getDataSource());

    @Override
    public void add(Category category) {
        da.update("insert into category(name,descript) value(?,?)", category.getName(), category.getDescript());
    }

    @Override
    public void update(Category category) {
        da.update("update category set name=?,descript=? where id=?", category.getName(), category.getDescript(), category.getId());
    }

    @Override
    public void delete(long id) {
        da.update("delete from category where id=?", id);
    }

    @Override
    public List get() {
        return (List) da.query("select * from category", new BeanListHandler(Category.class));
    }

    @Override
    public Category getbyId(long id) {
        return (Category) da.query("select * from category where id=?", new BeanHandler(Category.class), id);
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        return null;
    }

    @Override
    public List<Category> getCategoryBooks() {
        return (List) da.query("SELECT id,name,descript from category INNER JOIN (select DISTINCT category_Id from books) b on b.category_Id=id", new BeanListHandler(Category.class));
    }
}
